<template>
  <div class="register-container">
    <!-- 注册表单 -->
    <div class="register-box">
      <h2>用户注册</h2>
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label>用户名：</label>
          <input v-model="formData.username" type="text" required>
        </div>

        <div class="form-group">
          <label>账户名：</label>
          <input v-model="formData.account" type="text" required>
        </div>

        <div class="form-group">
          <label>密码：</label>
          <input v-model="formData.password" type="password" required>
        </div>

        <div class="form-group">
          <label>确认密码：</label>
          <input v-model="confirmPassword" type="password" required>
        </div>

        <div class="form-group">
          <label>姓名：</label>
          <input v-model="formData.name" type="text" required>
        </div>

        <div class="form-group">
          <label>性别：</label>
          <select v-model="formData.gender">
            <option value="男">男</option>
            <option value="女">女</option>
          </select>
        </div>

        <div class="form-group">
          <label>年龄：</label>
          <input v-model.number="formData.age" type="number" min="0" required>
        </div>

        <button type="submit">注册</button>
        <p class="login-link">
          已有账号？<a @click="$router.push('/')">立即登录</a>
        </p>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router = useRouter()
const formData = ref({
  username: '',
  account: '',
  password: '',
  name: '',
  gender: '男',
  age: 18
})
const confirmPassword = ref('')

const handleSubmit = () => {
  if (!formData.value.username || !formData.value.account) {
    alert('用户名和账户名不能为空');
    return;
  }
  // 表单验证
  if (formData.value.password !== confirmPassword.value) {
    alert('两次输入的密码不一致')
    return
  }

  // 发送注册请求
  axios.post('/register', {
    ...formData.value,
    userRole: '购票者' // 固定普通用户角色
  })
      .then(() => {
        alert('注册成功，请登录')
        router.push('/')
      })
      .catch(error => {
        console.error('注册失败:', error)
        alert('注册失败，请重试')
      })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f5f7fa;
}

.register-box {
  background: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
  width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
}

input, select {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

button {
  width: 100%;
  padding: 10px;
  background: #3b99fc;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
}

button:hover {
  background: #2676e3;
}

.login-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
}
</style>
